package com.springboot.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.config.SocketConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

/**
 * @Author: TongRui乀
 * @Date: 2019/6/21 16:10
 * @description：
 */
public class MapUtils {


    public static JSONObject getByCity(String address){

        String url = "https://apis.map.qq.com/ws/geocoder/v1/?address="+address+"&key=RIPBZ-AQZ3J-64DF6-FAQMO-3Q5YV-SFFDB&region=台湾";

        JSONObject results = new JSONObject();
        // 调用腾讯地图开放API
        CloseableHttpClient httpClient = null;
        try {
            RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
            //客户端和服务器建立连接的timeout
            requestConfigBuilder.setConnectTimeout(30000);
            //从连接池获取连接的timeout
            requestConfigBuilder.setConnectionRequestTimeout(30000);
            //连接建立后，request没有回应的timeout
            requestConfigBuilder.setSocketTimeout(30000);

            HttpClientBuilder clientBuilder = HttpClientBuilder.create();
            clientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());
            //连接建立后，request没有回应的timeout
            clientBuilder.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(30000).build());

            httpClient = clientBuilder.build();

            URIBuilder uriBuilder = new URIBuilder(url);

            HttpGet httpGet = new HttpGet(uriBuilder.build());

            CloseableHttpResponse httpResponse = null;

            // 执行请求并获得响应结果
            httpResponse = httpClient.execute(httpGet);

            String content = "";

            if (httpResponse.getEntity() != null) {
                content = EntityUtils.toString(httpResponse.getEntity());
            }

            JSONObject txResponse = JSONObject.parseObject(content);

            JSONObject result = (JSONObject) txResponse.get("result");

            // 区/ 县 编码
            String code = (String) ((JSONObject)result.get("ad_info")).get("adcode");

            JSONObject location = result.getJSONObject("location");

            results.put("code", code);
            results.put("location", location);

            // 根据区县的code 查询区 市 省 的信息
        } catch (Exception e) {
            e.printStackTrace();
        }

        return results;

    }


    public static JSONArray getChild(String id){

        String url = "https://apis.map.qq.com/ws/district/v1/getchildren?key=RIPBZ-AQZ3J-64DF6-FAQMO-3Q5YV-SFFDB&id=" + id;

        JSONArray results = new JSONArray();
        // 调用腾讯地图开放API
        CloseableHttpClient httpClient = null;
        try {
            RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
            //客户端和服务器建立连接的timeout
            requestConfigBuilder.setConnectTimeout(30000);
            //从连接池获取连接的timeout
            requestConfigBuilder.setConnectionRequestTimeout(30000);
            //连接建立后，request没有回应的timeout
            requestConfigBuilder.setSocketTimeout(30000);

            HttpClientBuilder clientBuilder = HttpClientBuilder.create();
            clientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());
            //连接建立后，request没有回应的timeout
            clientBuilder.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(30000).build());

            httpClient = clientBuilder.build();

            URIBuilder uriBuilder = new URIBuilder(url);

            HttpGet httpGet = new HttpGet(uriBuilder.build());

            CloseableHttpResponse httpResponse = null;

            // 执行请求并获得响应结果
            httpResponse = httpClient.execute(httpGet);

            String content = "";

            if (httpResponse.getEntity() != null) {
                content = EntityUtils.toString(httpResponse.getEntity());
            }

            JSONObject txResponse = JSONObject.parseObject(content);

            results = txResponse.getJSONArray("result");

        } catch (Exception e) {
            e.printStackTrace();
        }

        return results;

    }


}
